#include <iostream>
#include <string>
using namespace std;

int x;
string s;

int main(){
	cin >> x >> s;
	int ret = 0,n = 1;
	for(int i = s.size() - 1;i >= 0;i--){
		if(s[i] >= 'A' && s[i] <= 'Z'){
			ret += (s[i] - 'A' + 10) * n;
		}
		else{
			ret += (s[i] - '0') * n;
		}
		n *= x;
	}
	cout << ret << endl;
	return 0;
}
